﻿using System;
using System.Collections;

namespace CSharpHW02
{
    public class ATM
    {
        CreateBank1 bank;

        public class BigMoneyArgs : EventArgs
        {
            public string AccountId { get; }
            public double Amount { get; }

            public BigMoneyArgs(string accountId, double amount)
            {
                AccountId = accountId;
                Amount = amount;
            }
        }

        public event EventHandler<BigMoneyArgs> BigMoneyFetched;

        public class BadCashException : Exception
        {
            public BadCashException(string message) : base(message) { }
        }

        public ATM(CreateBank1 bank)
        {
            this.bank = bank;
        }

        public void checkAccount()
        {
            Console.WriteLine("请输入银行账号：");
            string id = Console.ReadLine();
            Console.WriteLine("请输入账号密码：");
            string pw = Console.ReadLine();

            Account Account = bank.getAccount(id, pw);

            if (Account == null)
            {
                Console.WriteLine("账号或密码错误");
                return;
            }
            Console.WriteLine("用户" + Account.id + " 欢迎您");
            Console.WriteLine("请选择操作：1.存款 2.取款 3.查询余额 4.退出");

            string choice = Console.ReadLine();
            if (choice == "1")
            {
                Console.WriteLine("请输入存款金额：");
                double money = double.Parse(Console.ReadLine());
                Account.Deposit(money);
                Console.WriteLine("存款成功");
                Console.WriteLine("当前余额为：" + Account.balance);
            }
            else if (choice == "2")
            {
                Console.WriteLine("请输入取款金额：");
                double money = double.Parse(Console.ReadLine());
                if (money > 10000)
                {
                    // 触发事件
                    BigMoneyFetched?.Invoke(this, new BigMoneyArgs(Account.id, money));
                }
                Account.Withdraw(money);
                Console.WriteLine("取款成功");
                Console.WriteLine("当前余额为：" + Account.balance);
            }
            else if (choice == "3")
            {
                Account.CheckBalance();
            }
            else if (choice == "4")
            {
                return;
            }
            else
            {
                Console.WriteLine("输入错误");
            }
        }

        public class Account
        {
            public string id;
            public string pw;
            public double balance;
            public Account(string id, string pw, double balance)
            {
                this.id = id;
                this.pw = pw;
                this.balance = balance;
            }

            public void Deposit(double amount)
            {
                balance += amount;
            }

            public void Withdraw(double amount)
            {
                Random r = new Random();
                if (r.Next(3) < 1)
                {
                    throw new BadCashException("取款时发现坏钞票，操作中止。");
                }
                balance -= amount;
            }

            public void CheckBalance()
            {
                Console.WriteLine("当前余额为：" + balance);
            }

            public bool Match(string id, string pw)
            {
                return this.id == id && this.pw == pw;
            }
        }

        public class CreateBank1
        {
            ArrayList accountList = new ArrayList();

            public Account CreateAccount(string id, string pw, double money)
            {
                Account a = new Account(id, pw, money);
                accountList.Add(a);
                return a;
            }

            public bool DeleteAccount(string id, string pw)
            {
                foreach (Account a in accountList)
                {
                    if (a.Match(id, pw))
                    {
                        accountList.Remove(a);
                        return true;
                    }
                }
                return false;
            }

            public Account getAccount(string id, string pw)
            {
                foreach (Account a in accountList)
                {
                    if (a.Match(id, pw))
                    {
                        return a;
                    }
                }
                return null;
            }
        }

        public class CreditAccount : Account
        {
            public double credit;
            public CreditAccount(string id, string pw, double balance, double credit) : base(id, pw, balance)
            {
                this.credit = credit;
            }
            public void CheckCredit()
            {
                Console.WriteLine("当前信用额度为：" + credit);
            }
            public void Withdraw(double amount)
            {
                if (balance + credit >= amount)
                {
                    Random r = new Random();
                    if (r.Next(3) < 1) // 1/3 的概率
                    {
                        throw new BadCashException("取款时发现坏钞票，操作中止。");
                    }
                    balance -= amount;
                }
                else
                {
                    Console.WriteLine("余额不足");
                }
            }
        }

        public class CreditBank : CreateBank1
        {
            ArrayList creditAccountList = new ArrayList();
            public CreditAccount CreateCreditAccount(string id, string pw, double money, double credit)
            {
                CreditAccount a = new CreditAccount(id, pw, money, credit);
                creditAccountList.Add(a);
                return a;
            }
            public bool DeleteCreditAccount(string id, string pw)
            {
                foreach (CreditAccount a in creditAccountList)
                {
                    if (a.Match(id, pw))
                    {
                        creditAccountList.Remove(a);
                        return true;
                    }
                }
                return false;
            }
            public CreditAccount getCreditAccount(string id, string pw)
            {
                foreach (CreditAccount a in creditAccountList)
                {
                    if (a.Match(id, pw))
                    {
                        return a;
                    }
                }
                return null;
            }
        }

        class Test
        {
            static void Main(string[] args)
            {
                ATM atm = new ATM(new CreateBank1());
                atm.bank.CreateAccount("123", "123", 100);

                atm.BigMoneyFetched += OnBigMoneyFetched;

                atm.checkAccount();
            }

            private static void OnBigMoneyFetched(object sender, ATM.BigMoneyArgs e)
            {
                Console.WriteLine($"警告：账户 {e.AccountId} 取款 {e.Amount} 元，超过 10000 元！");
            }
        }
    }
}